package com.itheima.dao;

import com.itheima.bean.Admin;
import com.itheima.bean.Course;

import java.io.*;
import java.util.ArrayList;

public class AdminDao {
    private static ArrayList<Course> list1 = new ArrayList<>();

    public ArrayList<Admin> redAdmin() throws IOException {
        ArrayList<Admin> list = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\admin.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            int i = Integer.parseInt(id);
            String password = split[1];
            Admin admin = new Admin(i, password);
            list.add(admin);
        }

        return list;
    }
    public boolean addcourse(Course course) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String type = split[2];
            Course course1 = new Course(id, name, type);
            list1.add(course1);
        }
        list1.add(course);
        BufferedWriter bu = new BufferedWriter(new FileWriter("menjinlin\\course.txt"));
        for (Course course1 : list1) {
            bu.write(course1.getCid() + "," + course1.getCname() + "," + course1.getCtype() + "\t\r");
            bu.flush();
        }

        return true;
    }

    public ArrayList<Course> findId() throws IOException {
        ArrayList<Course> tab = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id = split[0];
            String name = split[1];
            String type = split[2];
            Course course = new Course(id, name, type);
            tab.add(course);
        }

        return tab;
    }

    public void deletecourse(String id) throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id1 = split[0];
            String name = split[1];
            String type = split[2];
            Course course1 = new Course(id1, name, type);
            list1.add(course1);
            for (int i = 0; i < list1.size(); i++) {
                Course course = list1.get(i);
                if (id.equals(course.getCid())) {
                    list1.remove(i);
                }
            }
        }
        BufferedWriter bu = new BufferedWriter(new FileWriter("menjinlin\\course.txt"));
        for (Course course1 : list1) {
            bu.write(course1.getCid() + "," + course1.getCname() + "," + course1.getCtype() + "\t\r");
            bu.flush();
        }

    }

    public void findcourse() throws IOException {
        BufferedReader bf = new BufferedReader(new FileReader("menjinlin\\course.txt"));
        String line;
        while ((line = bf.readLine()) != null) {
            String[] split = line.split(",");
            String id1 = split[0];
            String name = split[1];
            String type = split[2];
            System.out.println(id1 + "\t" + name + "\t" + type);

        }
    }
}